package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("删除图书");
        System.out.print("请输入书名-->");
        String name = scanner.nextLine();
        Book book = null;
        int i = 0;
        for (i = 0; i < bookList.getBookNum(); i++) {
            book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                break;
            }
        }
        if(bookList.getBookNum() == i) {
            System.out.println("查无此书");
            return;
        } else {
            for (int j = i; j < bookList.getBookNum() - 1; j++) {
                bookList.setBook(bookList.getBook(i+1),i);
            }
        }
        bookList.setBook(null, bookList.getBookNum());
        bookList.setBookNum(bookList.getBookNum()-1);
        System.out.println("删除成功");

    }
}
